package com.dslu.fx.service.impl;

import com.dslu.fx.dao.DictDao;
import com.dslu.fx.dao.DzpAreaDao;
import com.dslu.fx.dao.EmergencyDepartDao;
import com.dslu.fx.dao.StockBillDao;
import com.dslu.fx.domain.AreaDO;
import com.dslu.fx.domain.DictDO;
import com.dslu.fx.domain.DzpAreaDO;
import com.dslu.fx.service.DzpService;
import com.dslu.fx.utils.CheckEmptyUtil;
import com.dslu.fx.utils.DrawTableImg;
import com.dslu.fx.utils.ListSplitUtils;
import com.dslu.fx.vo.BigDzpVO;
import com.dslu.fx.vo.EmergencyDepartVO;
import com.dslu.fx.vo.ProdVO;
import onbon.y2.Y2DynamicManager;
import onbon.y2.Y2Exception;
import onbon.y2.Y2Screen;
import onbon.y2.play.DynamicArea;
import onbon.y2.play.DynamicPlayFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DzpServiceImpl implements DzpService{

    @Autowired
    private DzpAreaDao dzpAreaDao;
    @Autowired
    private StockBillDao stockBillDao;
    @Autowired
    private EmergencyDepartDao emergencyDepartDao;

    @Autowired
    private DictDao dictDao;

    @Override
    public void initializeDzp() throws Y2Exception {
        List<DzpAreaDO> dzpAreaList = dzpAreaDao.getDzpAreaListByCode("ZQBZ");
        for (DzpAreaDO dzpAreaDO : dzpAreaList) {
            showStock(dzpAreaDO);
        }
    }

    @Override
    public List<ProdVO> flushDzpByAreaId(Integer areaId) throws Y2Exception {
        Map map=new HashMap();
        map.put("areaId",areaId);
        DzpAreaDO one = dzpAreaDao.getOne(map);
        List<ProdVO> prodVOS = showStock(one);
        return prodVOS;
    }

    @Override
    public List<EmergencyDepartVO> flushEmergencyDzp(Map<String, Object> params) throws Y2Exception {
        //params包含库区和紧急出库单
        //List<EmergencyDepartVO> emergencyDepart = emergencyDepartDao.getEmergencyDepart(params);
        List<EmergencyDepartVO> emergencyDepart=null;
        DzpAreaDO one = dzpAreaDao.getOne(params);
        if(one!=null){
            emergencyDepart= showEmergencyDepart(one,params);
        }

        return emergencyDepart;
    }

    @Override
    public AreaDO getAreaByName(String name) {
        return dzpAreaDao.getAreaByName(name);
    }

    @Override
    public void flushBigEmergencyDzp(BigDzpVO bigDzpVO) throws Y2Exception, IOException {
        Map map=new HashMap();//bigDzpIp
        map.put("key","bigDzpIp");
        DictDO one = dictDao.getOne(map);

       if(one!=null){
           showBigDzpEmergency(bigDzpVO,one.getValue());
       }

    }

    private void showBigDzpEmergency(BigDzpVO bigDzpVO,String ip) throws Y2Exception, IOException {
        Y2Screen screen = getScreenByIp(ip);
        String picPath="d:\\bigDzp.jpg";
        createBigImgEmergency(bigDzpVO,picPath);
        List<String> picPaths=new ArrayList<>();
        picPaths.add(picPath);
        doSend(screen,picPaths);
    }

    private  List<EmergencyDepartVO> showEmergencyDepart(DzpAreaDO one,Map<String, Object> params) throws Y2Exception {
        List<EmergencyDepartVO> emergencyDepart = emergencyDepartDao.getEmergencyDepart(params);
        if(CheckEmptyUtil.isNotEmpty(emergencyDepart)){
            Y2Screen screen = getScreenByIp(one.getIp());
            //Y2Screen screen=null;
            sendEmergency(screen,emergencyDepart,one.getAreaName());
        }
        return emergencyDepart;
    }

    private void sendEmergency(Y2Screen screen, List<EmergencyDepartVO> emergencyDepart, String areaName) {
        try {
            List<List<EmergencyDepartVO>> partition = ListSplitUtils.partition(emergencyDepart, 5);
            //图文地址
            List<String> picStr=new ArrayList<>();
            for (int i=0;i<partition.size();i++){
                String picPath="d:\\"+areaName+i+".jpg";
                createImgEmergency(partition.get(i),picPath);
                picStr.add(picPath);
            }

            if(CheckEmptyUtil.isNotEmpty(picStr)){
                doSend(screen,picStr);
            }

        }catch (Exception e){
            e.printStackTrace();
        }
    }


    private List<ProdVO> showStock(DzpAreaDO dzpAreaDO) throws Y2Exception {
        List<ProdVO> prodVO = stockBillDao.getStockByAreaId(dzpAreaDO.getAreaId());
        if(CheckEmptyUtil.isNotEmpty(prodVO)){
            Y2Screen screen = getScreenByIp(dzpAreaDO.getIp());
            //Y2Screen screen =null;
            send(screen,prodVO,dzpAreaDO.getAreaName());
        }

        return prodVO;
    }


    public Y2Screen getScreenByIp(String ip) throws Y2Exception {
        Y2Screen screen = new Y2Screen(ip);
        screen.login("guest", "guest");
        screen.clearPlayResources();//清除播放资源（删除播放节目）
        return screen;
    }

    public  void send(Y2Screen screen, List<ProdVO> list,String areaName){
        try {
            List<List<ProdVO>> partition = ListSplitUtils.partition(list, 5);
            //图文地址
            List<String> picStr=new ArrayList<>();
            for (int i=0;i<partition.size();i++){
                String picPath="d:\\"+areaName+i+".jpg";
                createImg(partition.get(i),picPath);
                picStr.add(picPath);
            }

            if(CheckEmptyUtil.isNotEmpty(picStr)){
                doSend(screen,picStr);
            }

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void doSend(Y2Screen screen, List<String> picStr) throws IOException, Y2Exception {
        // 动态区
        // 取得管理程序
        Y2DynamicManager dyn = screen.dynamic();
        // 建立动态节目
        DynamicPlayFile file = new DynamicPlayFile();
        // 于节目中新增一个动态区
        DynamicArea darea = file.createArea(0,0,100,40);
       for (String pic:picStr){
           darea.addImage(pic);
       }
        // 将动态区上传
        dyn.write(file);
    }

    private void createBigImgEmergency(BigDzpVO bigDzpVO,String picPath) throws IOException {
        DrawTableImg.myGraphicsGeneration6(bigDzpVO, picPath);
    }

    private void createImgEmergency(List<EmergencyDepartVO> childs,String picPath) throws IOException {
        String s[][] ;
        s = new String[childs.size()+1][] ;
        s[0] = new String[] {"库位名称","状态"};
        for(int i=0;i<childs.size();i++){
            s[i+1]=new String[2];
            s[i+1][0]=childs.get(i).getDepartName();
            String status = childs.get(i).getStatus() == 0 ? "待出库" : "已出库";
            s[i+1][1]=status;
        }
        DrawTableImg.myGraphicsGeneration4(s, picPath);
    }

    private void createImg(List<ProdVO> childs,String picPath) throws IOException {
        String s[][] ;
        s = new String[childs.size()+1][] ;
        s[0] = new String[] {"物资名称","数量","规格","单位"};
        for(int i=0;i<childs.size();i++){
            s[i+1]=new String[4];
            s[i+1][0]=childs.get(i).getProdName();
            s[i+1][1]=childs.get(i).getNum()+"";
            s[i+1][2]=childs.get(i).getSpecifications();
            s[i+1][3]=childs.get(i).getEmit();
        }
        DrawTableImg.myGraphicsGeneration4(s, picPath);
    }

}
